package com.snack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snack.common.result.Result;
import com.snack.entity.pojo.Snack;
import com.snack.mapper.SnackMapper;
import com.snack.service.BatchService;
import com.snack.service.BatchSnackService;
import com.snack.service.SnackService;
import com.snack.service.StoreService;
import com.snack.utils.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangfuhao
 * @Date 2025/3/28
 */
@Service
@Slf4j
public class SnackServiceImpl extends ServiceImpl<SnackMapper, Snack> implements SnackService {


    @Resource
    private StoreService storeService;

    @Override
    public Result addSnack(Snack snack) {
        //获取员工所在店铺零食
        Long storeId = storeService.getStoreIdByUser();
        snack.setStoreId(storeId);
        //商家为自己店铺新增零食接口

        log.info("店铺保存零食信息：{}",snack);

        //根据id查询该零食，存在则直接返回错误信息
        if (snack.getId() != null) {
            Snack snack1 = getById(snack.getId());
            if (snack1 != null) {
                return Result.error("该零食已存在，可直接添加库存");
            }
        }


        //先查询该零食在该店铺是否存在
        Snack one = lambdaQuery()
                .eq(Snack::getBrand, snack.getBrand())
                .eq(Snack::getCategoryId, snack.getCategoryId())
                .eq(Snack::getStoreId, snack.getStoreId())
                .eq(Snack::getName, snack.getName())
                .eq(Snack::getFlavor, snack.getFlavor())
                .eq(Snack::getSpecifications, snack.getSpecifications())
                .eq(Snack::getNutritionInfo, snack.getNutritionInfo())
                .eq(Snack::getShelfLife, snack.getShelfLife())
                .eq(Snack::getPrice, snack.getPrice())
                .one();
        if (one!= null){
            return Result.error("该零食已存在，可直接添加库存");
        }

        snack.setInventory(0);
        boolean save = save(snack);
        if (!save){
            return Result.error("零食添加失败");
        }
        //不存在即可保存零食信息
        return Result.success(snack);
    }

    @Override
    public Result deleteSnack(Snack snack) {

        Snack snack1 = getById(snack.getId());
        log.info("删除零食：{}",snack.getId());
        boolean b = removeById(snack);
        if (!b){
            return Result.success("删除失败");
        }
        return Result.success(snack1);
    }

    @Override
    public Result listSnack(Snack snack) {

        List<Snack> list = lambdaQuery()
                .like(StringUtils.isNotNonOrEmpty(snack.getName()), Snack::getName, "%" + snack.getName() + "%")
                .like(StringUtils.isNotNonOrEmpty(snack.getFlavor()), Snack::getFlavor, "%" + snack.getFlavor() + "%")
                .like(StringUtils.isNotNonOrEmpty(snack.getSpecifications()), Snack::getSpecifications, "%" + snack.getSpecifications() + "%")
                .like(StringUtils.isNotNonOrEmpty(snack.getNutritionInfo()), Snack::getNutritionInfo, "%" + snack.getNutritionInfo() + "%")
                .like(StringUtils.isNotNonOrEmpty(snack.getBrand()), Snack::getBrand, "%" + snack.getBrand() + "%")
                .eq(snack.getCategoryId() != null, Snack::getCategoryId, snack.getCategoryId())
                .eq(snack.getStoreId() != null,Snack::getStoreId,snack.getStoreId())
                .eq(snack.getId() != null, Snack::getId, snack.getId())
                .list();

        if (list==null||list.isEmpty()){
            return Result.success(new ArrayList<>());
        }

         return Result.success(list);

    }


    @Override
    public Result updateSnack(Snack snack) {
        boolean b = updateById(snack);
        if (!b){
            return Result.error("更新失败");
        }
        return Result.success(snack);
    }

    @Override
    public Result getSnackById(Integer id) {
        Snack snack = getById(id);
        if (snack == null) {
            return Result.error("零食不存在");
        }
        return Result.success(snack);
    }

    @Override
    public Result getLowStockSnacks() {
        Long storeId = storeService.getStoreIdByUser();
        QueryWrapper<Snack> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId).lt("inventory", 20);
        List<Snack> lowStockSnacks = list(queryWrapper);
        return Result.success(lowStockSnacks);
    }




}